<template>
  <div>
    我的文件
  </div>
  <div class="tools-div">
    <el-button type="success" size="small" @click="addShow">添 加</el-button>
  </div>
  <!--  添加-->
  <el-dialog v-model="dialogVisible" title="添加" width="30%">
    <el-form label-width="120px">
      <el-form-item label="文件名称">
        <el-input v-model="fileManage.fileName" />
      </el-form-item>
      <el-form-item label="上传文件">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8082/tFileManage/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :headers="headers"
        >
          <img
            v-if="fileManage.original"
            :src="fileManage.original"
            class="avatar"
          />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveFileManage">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <!--  列表-->
  <el-table :data="fileList" style="width: 100%">
    <el-table-column prop="id" label="文件编号" width="85" />
    <el-table-column prop="fileName" label="文件名称" />
    <el-table-column prop="" label="文件展示" #default="scope">
      <el-image
        style="width: 100px; height: 100px"
        :src="scope.row.original"
        :fit="scope.row.bucketName"
      />
    </el-table-column>
    <el-table-column prop="" label="文件大小">
      <template #default="scope">
        {{ formatFileSize(scope.row.fileSize) }}
      </template>
    </el-table-column>
    <el-table-column prop="type" label="文件类型" />
    <el-table-column prop="tenantName" label="所属租户" />
    <el-table-column prop="createName" label="创建人" />
    <el-table-column prop="createTime" label="上传时间" />
    <el-table-column prop="updateTime" label="修改人" />
    <el-table-column prop="updateTime" label="修改时间" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button
          size="small"
          type="success"
          @click="xiazai(scope.row.original)"
        >
          下载
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
<script setup>
import { ref } from 'vue'
import { fileListPage, deleteUpload, save, update } from '@/api/file'
import { ElMessage } from 'element-plus'
// 计算大小格式
function formatFileSize(size) {
  if (size === null || size === undefined) {
    return '未知大小'
  }
  if (typeof size !== 'number') {
    return '未知大小'
  }
  const units = ['B', 'KB', 'MB', 'GB', 'TB']
  let unitIndex = 0
  while (size >= 1024 && unitIndex < units.length - 1) {
    size /= 1024
    unitIndex++
  }
  return size.toFixed(2) + units[unitIndex]
}
const fileList = ref([])
// 查询所有
const handleChange = () => {
  fileListPage().then(res => {
    console.log(res)
    fileList.value = res.data
  })
}
handleChange()

const dialogVisible = ref(false)
const fileManage = ref({
  id: null,
  fileName: '',
  original: '',
  bucketName: '',
  type: '',
  fileSize: null,
})
const dialogVisible1 = ref(false)

const handleAvatarSuccess = response => {
  // console.log("------------");
  // console.log(response.msg);
  fileManage.value.original = response.msg
}
const headers = {
  // 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
  token: '[' + localStorage.getItem('VEA-TOKEN') + ']',
}
// 添加弹窗
const addShow = () => {
  fileManage.value = {}
  dialogVisible.value = true
}
const saveFileManage = () => {
  console.log(fileManage.value)
  save(fileManage.value).then(res => {
    console.log(res)
    if (res.code == 200) {
      ElMessage.success('添加成功')
      dialogVisible.value = false
      fileManage.value = {}
      handleChange()
    }
  })
}
// 添加
const saveFileManage1 = () => {
  console.log(fileManage.value)
  update(fileManage.value).then(res => {
    console.log(res)
    if (res.code == 200) {
      ElMessage.success('修改成功')
      dialogVisible1.value = false
      fileManage.value = {}
      handleChange()
    }
  })
}
// 下载
const xiazai = url => {
  window.open(url)
}
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
